package com.demo.commonsio;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class BigFileGenerater {

	public static void main(String[] args) throws IOException {
		BigFileGenerater b = new BigFileGenerater();
		b.writeToFileByNetwork("d://a.txt", "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=94296483_hao_pg&wd=java nio 写文件&oq=java nio&rsv_pq=fe87af800006a649&rsv_t=2e15M13fa9S09mTO2eiCLPwdW7fzvAcXskSeY3aXndGmmSBV6vsvJIHl8qOuXTKZuM6G3KAA&rqlang=cn&rsv_enter=1&rsv_sug3=21&rsv_sug1=14&rsv_sug7=100&bs=java nio", 10);
	}
	
	public void writeToFileByNetwork(String fileName, String dataUrl, long fileSize) throws IOException{
		URL url = new URL(dataUrl);
		writeToFileByInputstream(fileName, url.openStream(), fileSize);
	}
	
	public void writeToFileByInputstream(String fileName, InputStream inputStream, long fileSize) throws IOException{
		byte[] data = new byte[512];
		while(inputStream.available()!=-1 ){
			inputStream.read(data);
			FileUtils.writeByteArrayToFile(new File(fileName), data, true);
		}
		inputStream.close();
	}

}
